/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "CoRadioButtonGroup.h"
#include "onyx/ui/buttons.h"

CoRadioButtonGroup::CoRadioButtonGroup(const QStringList& texts, QWidget* parent)
: QWidget(parent),
m_layout(this)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    setLayout(&m_layout);

    m_count = texts.count();

    for (int i = 0; i < m_count; i++)
    {
        ui::OnyxCheckBox* checkBox = new ui::OnyxCheckBox(texts[i], this);
        m_layout.addWidget(checkBox);
        m_buttonGroup.addButton(checkBox, i);
    }

    Out_Log(__FUNCTION__, COCLASSNAME);
}

int CoRadioButtonGroup::selected()
{
    return m_buttonGroup.checkedId();
}

void CoRadioButtonGroup::select(int id)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    m_buttonGroup.button(id)->setChecked(true);

    Out_Log(__FUNCTION__, COCLASSNAME);
}

bool CoRadioButtonGroup::focusePrev()
{
    int id = m_buttonGroup.id(static_cast<QAbstractButton*>(QApplication::focusWidget()));

    if (id == 0)
    {
        return true;
    }

    m_buttonGroup.button(id - 1)->setFocus();
    return false;
}

bool CoRadioButtonGroup::focuseNext()
{
    int id = m_buttonGroup.id(static_cast<QAbstractButton*>(QApplication::focusWidget()));

    if (id == m_count - 1)
    {
        return true;
    }

    m_buttonGroup.button(id + 1)->setFocus();
    return false;
}
